package alex.rsx.view;

import java.util.ArrayList;
import java.util.List;

import alex.rsx.activity.r.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DiffuseView extends RelativeLayout implements OnClickListener {

	private final static int RADIUS = 200;
	private Button mMain, bt1, bt2, bt3, bt4, bt5, bt6, bt_move;
	
	private int duratime = 500;
	private LinearLayout mB1;
	private LinearLayout mB2;
	private LinearLayout mB3;
	private LinearLayout mB4, mB5, mB6, boss;
	private List<AnimationSet> mOutAnimatinSets = new ArrayList<AnimationSet>();
	private List<AnimationSet> mInAnimatinSets = new ArrayList<AnimationSet>();
	private List<LinearLayout> mList = new ArrayList<LinearLayout>();
	private boolean isIn = true;
	private boolean isFirstInitAnim = true;
	private boolean isEnd = false;
	private boolean isClick = false;
	
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private OnClickListener mClickListener;
	
	public DiffuseView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initView(context);
	}

	public DiffuseView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView(context);
	}
	
	public DiffuseView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		initView(context);
	}
	
	
	private void initView(Context context){
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		LinearLayout ml = (LinearLayout)mLayoutInflater.inflate(R.layout.view_diffuse, null);
		mMain = (Button) ml.findViewById(R.id.main);
		mB1 = (LinearLayout) ml.findViewById(R.id.b1);
		mB2 = (LinearLayout) ml.findViewById(R.id.b2);
		mB3 = (LinearLayout) ml.findViewById(R.id.b3);
		mB4 = (LinearLayout) ml.findViewById(R.id.b4);
		mB5 = (LinearLayout) ml.findViewById(R.id.b5);
		mB6 = (LinearLayout) ml.findViewById(R.id.b6);
		boss = (LinearLayout) ml.findViewById(R.id.boss);

		bt1 = (Button) ml.findViewById(R.id.bt1);
		bt2 = (Button) ml.findViewById(R.id.bt2);
		bt3 = (Button) ml.findViewById(R.id.bt3);
		bt4 = (Button) ml.findViewById(R.id.bt4);
		bt5 = (Button) ml.findViewById(R.id.bt5);
		bt6 = (Button) ml.findViewById(R.id.bt6);

		mB1.setVisibility(View.INVISIBLE);
		mB2.setVisibility(View.INVISIBLE);
		mB3.setVisibility(View.INVISIBLE);
		mB4.setVisibility(View.INVISIBLE);
		mB5.setVisibility(View.INVISIBLE);
		mB6.setVisibility(View.INVISIBLE);

		mList.add(mB1);
		mList.add(mB2);
		mList.add(mB3);
		mList.add(mB4);
		mList.add(mB5);
		mList.add(mB6);

		bt1.setOnClickListener(mClickListener);
		bt2.setOnClickListener(mClickListener);
		bt3.setOnClickListener(mClickListener);
		bt4.setOnClickListener(mClickListener);
		bt5.setOnClickListener(mClickListener);
		bt6.setOnClickListener(mClickListener);
		mMain.setOnClickListener(this);
		this.addView(ml,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		System.out.println("boss:left=" + boss.getLeft() + " top="
				+ boss.getTop() + " right=" + boss.getRight() + " bottom="
				+ boss.getBottom());
		if (isFirstInitAnim) {
			initAnimation();
			isFirstInitAnim = false;
		}
		if (v.getId() != mMain.getId()) {
			getFocus(v);
			// Toast.makeText(Main.this, "" + v.getId(), 0).show();
		}
		if (v.getId() == mMain.getId()) {
			if (!isEnd) {
				if (v.getAnimation() == null) {
					final float degree;
					if (isIn) {
						degree = 360f;
					} else {
						degree = -360f;
					}
					if (v.requestFocus()) {
						RotateAnimation rotateAnimation = new RotateAnimation(
								0f, degree, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						rotateAnimation.setDuration(500);
						v.startAnimation(rotateAnimation);
						if (isIn) {
							isIn = false;
							isEnd = true;
							startOutAnimation();
						} else {
							isIn = true;
							startInAnimation();
						}
					} else {
						v.setOnFocusChangeListener(new View.OnFocusChangeListener() {

							@Override
							public void onFocusChange(View v, boolean hasFocus) {
								// TODO Auto-generated method stub
								if (hasFocus) {
									RotateAnimation rotateAnimation = new RotateAnimation(
											0f, degree,
											Animation.RELATIVE_TO_SELF, 0.5f,
											Animation.RELATIVE_TO_SELF, 0.5f);
									rotateAnimation.setDuration(500);
									v.startAnimation(rotateAnimation);
									if (isIn) {
										isEnd = true;
										isIn = false;
										startOutAnimation();
									} else {
										isIn = true;
										startInAnimation();
									}
								}
							}
						});
						getFocus(v);
					}

					/*
					 * rotateAnimation.setFillEnabled(true);
					 * rotateAnimation.setFillAfter(true);
					 */

				}
			}
		}
	
	}
	

	private void startInAnimation() {
		for (int i = 0; i < mList.size(); i++) {
			LinearLayout layout = mList.get(i);
			layout.startAnimation(mInAnimatinSets.get(i));
		}
	}

	private void startOutAnimation() {
		for (int i = 0; i < mList.size(); i++) {
			LinearLayout layout = mList.get(i);
			if (layout.getVisibility() == View.INVISIBLE) {
				layout.setVisibility(View.VISIBLE);
			}
			layout.startAnimation(mOutAnimatinSets.get(i));
		}
	}

	private void getFocus(View view) {
		if (view != null) {
			if (!view.requestFocus()) {
				view.setFocusable(true);
				view.setFocusableInTouchMode(true);
				view.requestFocus();
			}
		}
	}
	
	private void initAnimation() {
		int size = mList.size();
		for (int i = 0; i < size; i++) {
			final LinearLayout layout = mList.get(i);
			int x = 0;
			int y = 0;
			double angle = 0;
			if (i == 0) {
				x = RADIUS;
				y = 0;
				angle = 0;
			} else if (i == size - 1) {
				x = -RADIUS;
				y = 0;
				angle = 0;
			} else if (i > 0 && i <= 2) {
				// angle = (90 / (size / 2 - 1)) * i;
				angle = (180 * i / (size - 1));
				x = (int) (RADIUS * Math.cos(Math.toRadians(angle)));
				y = -(int) (RADIUS * Math.sin(Math.toRadians(angle)));
			} else if (i > 2 && i < size - 1) {
				// angle = (90 / (size / 2 - 1)) * (i - 3);
				angle = (180 * i) / (size - 1);
				x = (int) (RADIUS * Math.cos(Math.toRadians(angle)));
				y = -(int) (RADIUS * Math.sin(Math.toRadians(angle)));
			}
			// long time = ANIMATION_TIME - i * TIME_INTERVAL;
			// final long time = 800;
			System.out.println("===============================");
			System.out.println(i + "====>" + x);
			System.out.println(i + "====>" + y);
			System.out.println(i + "====>" + (angle));
			final int left = boss.getLeft() + x * 2 / 3;
			final int top = boss.getTop() + y * 2 / 3;
			final int right = boss.getRight() + x * 2 / 3;
			final int bottom = boss.getBottom() + y * 2 / 3;

			TranslateAnimation outTranAni = new TranslateAnimation(0, x, 0, y);
			outTranAni.setFillAfter(true);
			outTranAni.setDuration(duratime);

			AnimationSet outSet = new AnimationSet(true);
			outSet.addAnimation(outTranAni);
			// outSet.addAnimation(outRotaAni);
			mOutAnimatinSets.add(outSet);
			//
			final AnimationSet outAfterSet = new AnimationSet(true);
			TranslateAnimation outAfterTranAni = new TranslateAnimation(x,
					x * 2 / 3, y, y * 2 / 3);
			outAfterTranAni.setFillAfter(true);
			outAfterTranAni.setDuration(duratime);
			outAfterSet.addAnimation(outAfterTranAni);
			outSet.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationEnd(Animation animation) {
					layout.startAnimation(outAfterSet);
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationStart(Animation animation) {
					if (layout != null) {
						Button button = null;
						if (layout.getId() == mB1.getId()) {
							button = (Button) layout.findViewById(R.id.bt1);
						} else if (layout.getId() == mB2.getId()) {
							button = (Button) layout.findViewById(R.id.bt2);
						} else if (layout.getId() == mB3.getId()) {
							button = (Button) layout.findViewById(R.id.bt3);
						} else if (layout.getId() == mB4.getId()) {
							button = (Button) layout.findViewById(R.id.bt4);
						} else if (layout.getId() == mB5.getId()) {
							button = (Button) layout.findViewById(R.id.bt5);
						} else if (layout.getId() == mB6.getId()) {
							button = (Button) layout.findViewById(R.id.bt6);
						}
						float degree;
						if (isIn) {
							degree = -720f;
						} else {
							degree = 720f;
						}
						RotateAnimation rotateanim = new RotateAnimation(0f,
								degree, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						rotateanim.setFillAfter(true);
						rotateanim.setDuration(duratime);
						if (button != null) {
							button.startAnimation(rotateanim);
						}
					}
				}

			});
			outAfterSet.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					if (layout != null) {
						layout.clearAnimation();
						layout.layout(left, top, right, bottom);
						TextView tv = null;
						if (layout.getId() == mB1.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv1);
						} else if (layout.getId() == mB2.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv2);
						} else if (layout.getId() == mB3.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv3);
						} else if (layout.getId() == mB4.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv4);
						} else if (layout.getId() == mB5.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv5);
						} else if (layout.getId() == mB6.getId()) {
							tv = (TextView) layout.findViewById(R.id.tv6);
						}
						tv.setVisibility(View.VISIBLE);
						AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
						alphaAnimation.setDuration(duratime);
						tv.startAnimation(alphaAnimation);
						isEnd = false;
					}
				}
			});
			//
			TranslateAnimation inTranAni = new TranslateAnimation(0,
					-x * 2 / 3, 0, -y * 2 / 3);
			/*
			 * inTranAni.setFillEnabled(true); inTranAni.setFillAfter(true);
			 */
			inTranAni.setFillAfter(true);
			inTranAni.setDuration(duratime);
			AnimationSet inSet = new AnimationSet(true);
			inSet.addAnimation(inTranAni);
			// inSet.addAnimation(inRotaAni);
			mInAnimatinSets.add(inSet);
			inSet.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
					if (layout != null) {
						Button button = null;
						TextView tView = null;
						if (layout.getId() == mB1.getId()) {
							button = (Button) layout.findViewById(R.id.bt1);
							tView = (TextView) layout.findViewById(R.id.tv1);
						} else if (layout.getId() == mB2.getId()) {
							button = (Button) layout.findViewById(R.id.bt2);
							tView = (TextView) layout.findViewById(R.id.tv2);
						} else if (layout.getId() == mB3.getId()) {
							button = (Button) layout.findViewById(R.id.bt3);
							tView = (TextView) layout.findViewById(R.id.tv3);
						} else if (layout.getId() == mB4.getId()) {
							button = (Button) layout.findViewById(R.id.bt4);
							tView = (TextView) layout.findViewById(R.id.tv4);
						} else if (layout.getId() == mB5.getId()) {
							button = (Button) layout.findViewById(R.id.bt5);
							tView = (TextView) layout.findViewById(R.id.tv5);
						} else if (layout.getId() == mB6.getId()) {
							button = (Button) layout.findViewById(R.id.bt6);
							tView = (TextView) layout.findViewById(R.id.tv6);
						}
						float degree;
						if (isIn) {
							degree = -720f;
						} else {
							degree = 720f;
						}
						RotateAnimation rotateanim = new RotateAnimation(0f,
								degree, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						rotateanim.setFillAfter(true);
						rotateanim.setDuration(duratime);
						if (button != null) {
							button.startAnimation(rotateanim);
						}
						tView.setVisibility(View.INVISIBLE);
					}
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// TODO Auto-generated method stub
					layout.layout(boss.getLeft(), boss.getTop(),
							boss.getRight(), boss.getBottom());
					layout.setVisibility(View.INVISIBLE);
					layout.clearAnimation();
						setVisibility(View.GONE);
				}
			});
		}
	}
}
